/**
 * 
 */
package vue.desktop;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;

import model.cnf.CNF;
import model.cnf.Stat;
import model.session.CNFUse;
import vue.popup.JPopupCNFUse;
import vue.popup.JPopupIGDeskCNFUse;
import vue.session.IGCNFUse;
import controller.ISessionManager;

/**
 * <p>
 * Represent a CNF on the {@link IGDesktop}.
 * </p>
 * <p>
 * Provide access to a pop up menu
 * </p>
 * <p>
 * can be moved
 * </p>
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGDeskCNFUse extends IGDeskItem implements MouseListener {

	private static final long		serialVersionUID	= 1L;

	private IGDeskCNFInformation	info;
	private IGDeskCNFStat			stat				= null;
	private IGCNFUse				cnfuse;

	protected JPopupCNFUse			rightMenu;
	private boolean					showStat			= false;

	/**
	 * Create a {@link IGDeskCNFUse}.
	 * 
	 * @param cnfuse
	 *            the {@link CNFUse} associate
	 * @param manager
	 *            the session manager
	 */
	public IGDeskCNFUse(IGCNFUse cnfuse, ISessionManager manager) {
		this.cnfuse = cnfuse;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// representation graphique de la cnf
		info = new IGDeskCNFInformation(cnfuse.getCnfuse());
		add(info);

		// pop up menu
		rightMenu = new JPopupIGDeskCNFUse(this, manager);
		addMouseListener(this);

		setBorder(BorderFactory.createLineBorder(Color.BLACK));

		setOpaque(false);
		setSize(getPreferredSize());
		setSelected(false);
	}

	/**
	 * Return the {@link IGCNFUse} associate.
	 * 
	 * @return the {@link IGCNFUse} associate.
	 */
	public IGCNFUse getCnfuse() {
		return cnfuse;
	}

	/**
	 * Refresh the information.
	 */
	public void refresh() {
		info.refresh(cnfuse.getCnfuse());
		Stat s = cnfuse.getCnfuse().getCnf().getStat();
		if (s == null)
		{
			if (stat != null)
				remove(stat);
		}
		else

			if (stat != null)
				stat.refresh(s);
			else
			{
				stat = new IGDeskCNFStat(s);
				add(stat);
				showStat = true;
			}

		setSize(getPreferredSize());
		// repaint();
		validate();

		rightMenu.refresh();
	}

	/**
	 * Call to show the stat of the {@link CNF} on the {@link IGDeskCNFUse}.
	 * 
	 * @param show
	 *            true or false
	 */
	public void showStat(boolean show) {
		if (show && !showStat && stat != null)
		{// peut montrer les stats
			add(stat);
			showStat = true;
			refresh();
			getParent().repaint();
			return;
		}
		if (!show && showStat && stat != null)
		{// peut masquer les stats
			remove(stat);
			showStat = false;
			refresh();
			getParent().repaint();
			return;
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		cnfuse.notifySelected();
	}


	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}

	@Override
	public void setSelected(boolean selected) {
		super.setSelected(selected);
		if (!selected)
			setBorder(null);
		setSize(getPreferredSize());
	}

	@Override
	public void setSecondSelected(boolean selected) {
		super.setSecondSelected(selected);
		if (!selected)
			setBorder(null);
		setSize(getPreferredSize());
	}

	/*
	 * @Override protected void paintComponent(Graphics g) {
	 * 
	 * super.paintComponent(g); g.setColor(getBackground()); g.fillRect(0, 0,
	 * g.getClipBounds().width, g.getClipBounds().height); // g.setColor(Color.black);
	 * 
	 * 
	 * }
	 */

	/**
	 * Return the showStat if the {@link IGDeskCNFUse} shows the stat.
	 * 
	 * @return the showStat
	 */
	public boolean getShowStat() {
		return showStat;
	}

	@Override
	public String toString() {
		return "IGDeskCNFUse : " + cnfuse.getCnfuse().getCnf().getFileName();
	}

}
